package dataStructure.stack;

public class ArrayToStackDemo {
    public static void main(String[] args) {
        ArrayToStack arrayToStack = new ArrayToStack(5);
        arrayToStack.push(1);
        arrayToStack.push(2);
        arrayToStack.push(3);
        arrayToStack.push(4);
        arrayToStack.print();
        System.out.println("-------------");
        arrayToStack.pop();
        arrayToStack.print();

    }
}
class ArrayToStack{
    private int sizeMax;
    public int top;
    public int[] data;
    ArrayToStack(int sizeMax){
        this.sizeMax = sizeMax;
        data = new int[this.sizeMax];
        top = -1;
    }

    public void push(int x){
        if (!isfull()){
            top++;
            data[top]=x;
        }

    }
    public int pop(){
        if (!isnull()){
            int value = data[top];
            top--;
            return value;
        }
        return -1;

    }
    public void print(){
        //从栈顶遍历
        for (int i =top;i>-1;i--){
            System.out.println(data[i]);
        }
    }

    public boolean isfull(){
        return top==sizeMax-1;
    }
    public boolean isnull(){
        return top==-1;
    }
}
